package com.ball.dev.controller;


import com.ball.common.dtos.ResponseResult;
import com.ball.common.utils.BeanUtils;
import com.ball.dev.entity.po.PostComment;
import com.ball.dev.entity.po.Teach;
import com.ball.dev.entity.vo.TeachVO1;
import com.ball.dev.service.IPostCommentService;
import com.ball.dev.service.ITeachService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 篮球实战教学
 *
 * @author lou
 * @since 2024-04-10
 */
@RestController
@RequestMapping("/teach")
@RequiredArgsConstructor
public class TeachController {

    private final ITeachService teachService;
    private final IPostCommentService postCommentService;
   @PostMapping("/add")
    public ResponseResult addTeach(@RequestBody Teach teach) {
        // 调用teachService的addTeach方法，传入teach参数，返回ResponseResult
        return teachService.addTeach(teach);
    }

    @GetMapping("/list")
    public ResponseResult getTeachList() {
        // 调用teachService的getTeachList方法，返回ResponseResult
        return teachService.getTeachList();
    }

    @GetMapping("/{id}")
    public ResponseResult getTeachById(@PathVariable String id) {
        // 根据id查询teach
        Teach teach = teachService.getById(id);
        // 如果查询结果为空，返回20001错误码
        if (teach == null){
            return ResponseResult.errorResult(20001,"数据不存在");
        }
        // 根据postId查询postComment，并按照创建时间降序排列
        List<PostComment> comments = postCommentService.lambdaQuery().eq(PostComment::getPostId, id).orderByDesc(PostComment::getCreateTime)
                .list();

        // 将teach转换为TeachVO1
        TeachVO1 teachVO1 = BeanUtils.copyBean(teach, TeachVO1.class);
        // 将查询结果赋值给teachVO1
        teachVO1.setComments(comments);

        // 返回teachVO1
        return ResponseResult.okResult(teachVO1);
    }


}
